package cn.momoshenchi.product.service.impl;

import cn.momoshenchi.product.dao.BrandDao;
import cn.momoshenchi.product.dao.CategoryDao;
import cn.momoshenchi.product.entity.BrandEntity;
import cn.momoshenchi.product.entity.CategoryEntity;
import cn.momoshenchi.product.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.common.utils.PageUtils;
import cn.momoshenchi.common.utils.Query;

import cn.momoshenchi.product.dao.CategoryBrandRelationDao;
import cn.momoshenchi.product.entity.CategoryBrandRelationEntity;
import cn.momoshenchi.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
@Autowired
private BrandDao brandDao;

@Autowired
private BrandService brandService;
@Autowired
private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation)
    {
        Long brandId = categoryBrandRelation.getBrandId();
        Long categoryId = categoryBrandRelation.getCategoryId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(categoryId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCategoryName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name)
    {
        CategoryBrandRelationEntity entity=new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        update(entity,new UpdateWrapper<CategoryBrandRelationEntity>()
        .eq("brand_id",brandId));
    }

    @Override
    public void updateCategory(Long catId, String name)
    {
        baseMapper.updateCategory(catId,name);
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId)
    {

        List<CategoryBrandRelationEntity> brandRelationEntities = list(new QueryWrapper<CategoryBrandRelationEntity>().eq("category_id", catId));
        List<Long> collect = brandRelationEntities.stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        List<BrandEntity> brandEntities = brandService.listByIds(collect);
        return  brandEntities;

    }

}